package com.angel.web.health;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.angel.core.Constants;
import com.angel.core.util.CodeUtil;
import com.angel.core.util.FileUpload;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.BuzExamination;
import com.angel.provider.health.BuzExaminationParsingProvider;
import com.angel.provider.health.BuzExaminationProvider;
import com.github.pagehelper.StringUtil;

/**
 * 体检报告部分
 */
@Controller
@RequestMapping("/examination")
public class BuzExaminationController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private BuzExaminationProvider provider;
	@Autowired
	private BuzExaminationParsingProvider parsingProvider;
	
	/**
	 * 搜索体检报告
	 * @param examination
	 * @param request
	 * @param pager
	 * @return
	 */
	@RequestMapping("/index")
	public String index(BuzExamination examination,HttpServletRequest request,Pager pager){
		examination.setEnable(1);
		Pager bean = provider.findAll(examination,pager);
		request.setAttribute("pager", bean);
		request.setAttribute("examination", examination);
		return "pomr/medical_record";
	}
	/**
	 * 跳转到添加页
	 * @return
	 */
	@RequestMapping("addPage")
	public String addPage(){
		return "pomr/medical_record_add";
	}
	
	/**
	 * 新增体检报告
	 * @param examination
	 * @param uploadFile
	 * @return
	 */
//	@RequestMapping("/save")
//	@ResponseBody
//	public String save(BuzExamination examination,MultipartFile uploadFile){
//		try {
//			String imgBaseUrl = PropertyConfUtil.getContextProperty("imgBaseUrl");
//			FastDFSClient client = new FastDFSClient();
//			String originalFilename = uploadFile.getOriginalFilename();
//			String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
//			String path = client.uploadFile(uploadFile.getBytes(), extName);
//			String uploadUrl = imgBaseUrl + path;
//			examination.setUploadUrl(uploadUrl);
//			int i = provider.insert(examination);
//			parsingProvider.save(uploadUrl,examination.getId());
//			return JsonUtil.toJson(i);
//		}  catch (Exception e) {
//			e.printStackTrace();
//			return JsonUtil.toJson(0);
//		}
//	}
	
	/**
	 * 
	 * @param cpr
	 * @param multiFileMap
	 *            所有上传文件
	 * @return
	 */
	@RequestMapping(value = "save")
	public String save(@ModelAttribute("examination")BuzExamination examination, HttpServletRequest request) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultiValueMap<String, MultipartFile> multiFileMap = multipartRequest.getMultiFileMap();
		try {
			String createBy = request.getSession().getAttribute("user").toString();
			examination.setUpdateBy(createBy);
			examination.setCreateBy(createBy);
			String uploadUrl = FileUpload.uploadForFastdfs(multiFileMap);
			examination.setUploadUrl(uploadUrl);
			int i = provider.insert(examination);
			examination.setCode(CodeUtil.createCode(null, Constants.EXAMINATION, examination.getId()));
			provider.updateById(examination);
			if(!StringUtil.isEmpty(uploadUrl)){
				logger.info("-------"+uploadUrl);
				parsingProvider.save(uploadUrl,examination.getId());
			}
		} catch (Exception e) {
			logger.info("pdf中包含图片，解析失败");
			return "redirect:/examination/index";
		}
		return "redirect:/examination/index";
	}
	
	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("del")
	public String del(String ids) {
		int delAll = provider.delAll(ids);
		if (delAll > 0) {
			return JsonUtil.toJson(delAll);
		} else {
			return JsonUtil.toJson(0);
		}
	}
}
